﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace GoogleMapsAPI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        public List<Data> treedata = new List<Data>();
        public List<Data> data = new List<Data>();
        private ucMaps mainMap;
        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadTreeView();
            
            mainMap = new GoogleMapsAPI.ucMaps(this);

            mainMap.Dock = System.Windows.Forms.DockStyle.Left;
            mainMap.Location = new System.Drawing.Point(0, 0);
            mainMap.Name = "mainMap";
            mainMap.Size = new System.Drawing.Size(640, 640);
            mainMap.TabIndex = 0;
            Controls.Add(this.mainMap);

            tbZoom.Value = mainMap.GetZoom();
        }

        public void SetTextboxMap(String lat, String lng)
        {
            textbox1.Text = lat;
            textBox2.Text = lng;
        }

        public void SetTextboxMarker(String lat, String lng)
        {
            textbox3.Text = lat;
            textBox4.Text = lng;
        }

        private void tbZoom_Scroll(object sender, EventArgs e)
        {
            mainMap.Zoom(tbZoom.Value);
        }

        public void LoadTreeView()
        {
            List<XElement> list = Data.ReadData();
            TreeNode node = new TreeNode("Địa Điểm");
            trwdiadiem.Nodes.Add(node);
            foreach (XElement element in list)
            {
                string temp = element.Attribute("name").Value;
                TreeNode parent = new TreeNode(temp);
                node.Nodes.Add(parent);
                foreach (XElement el in element.Elements())
                {
                    parent.Nodes.Add(el.Value);
                    Data item = new Data();
                    item.Group = temp;
                    string lat = el.Attribute("lat").Value;
                    string lng = el.Attribute("lng").Value;
                    item.Name = el.Value;
                    item.Lat = lat;
                    item.Lng = lng;
                    treedata.Add(item);
                }
                
            }
        }
        public void clearTree()
        {
            trwdiadiem.Nodes.Clear();
        }
        private void btnMarker_Click(object sender, EventArgs e)
        {
            frmMarker frm = new frmMarker(this);

            frm.lat = textbox3.Text;
            frm.lng = textBox4.Text;
            frm.ShowDialog();
        }

        private void btnhienthi_Click(object sender, EventArgs e)
        {
            data.Clear();
            data = treedata;
            mainMap.loadMap();
        }

        private void trwdiadiem_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            String text = e.Node.Text;
            int i;
            for (i = 0; i < treedata.Count; i++)
                if (treedata[i].Name == e.Node.Text && treedata[i].Group == e.Node.Parent.Text)
                    break;

            for (int j = 0; j < data.Count; j++)
                if (data[j].Name == treedata[i].Name && data[j].Group == treedata[i].Group)
                {
                    data.Remove(data[j]);
                    mainMap.loadMap();
                    return;
                }
                
            data.Add(treedata[i]);
            mainMap.loadMap();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Data old = new Data();
            if (trwdiadiem.SelectedNode == null)
                return;
            old.Name = trwdiadiem.SelectedNode.Text;
            if (old.Name == "Địa Điểm")
                return;
            old.Group = trwdiadiem.SelectedNode.Parent.Text;
            if (old.Group == "Địa Điểm")
            {
                frmcapnhatnhom f = new frmcapnhatnhom(this, old.Group);
                f.ShowDialog();
            }
            else
            {
                var p = treedata.Where(c => c.Group == old.Group && c.Name == old.Name).Single();
                old.Lat = p.Lat;
                old.Lng = p.Lng;
                frmcapnhat f = new frmcapnhat(this, old, textbox3.Text, textBox4.Text);
                f.ShowDialog();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Data old = new Data();
            if (trwdiadiem.SelectedNode == null)
                return;
            old.Name = trwdiadiem.SelectedNode.Text;
            if (old.Name == "Địa Điểm")
                return;
            old.Group = trwdiadiem.SelectedNode.Parent.Text;
            if (old.Group == "Địa Điểm")
            {
                if (MessageBox.Show("Xóa nhóm địa điểm \"" + old.Group + "\" ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        Data.DeletePlaceGroup(old.Group);
                        MessageBox.Show("Succeed.");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Fail.");
                    }
                }
            }
            else
            {
                if (MessageBox.Show("Xóa địa điểm \"" + old.Name + "\" ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        Data.DeletePlace(old);
                        MessageBox.Show("Succeed.");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Fail");
                    }
                }
            }
            
        }
    }
    
}
